package com.fanfanwang.ffw.app.ui.service;

import java.util.ArrayList;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.adapter.ServiceCarWashListAdapter;
import com.fanfanwang.ffw.app.adapter.ServiceYuYueListAdapter;
import com.fanfanwang.ffw.app.asynctask.GetOrdersListAsyncTask;
import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.bean.DianListBean;
import com.fanfanwang.ffw.app.bean.OrdersListBean;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.tools.TimeUtils;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 服务的预约列表
 * 
 * @author Administrator
 * 
 */
public class ServiceYuYueListAct extends BaseActivity {
	private ServiceYuYueListAct mAct;
	private CommonTitleBar commonTitleBar;
	public static PullToRefreshListView mPullToRefreshListView;
	//	public PersonalMoneyListAdapter listAdapter;
	//	private ArrayList<PersonalMoneyBean> personalMoneyBeans;
	private ListView listView;
	private ServiceYuYueListAdapter adapter;
	public int page=1;
	public static IUpdateServiceYuYue iUpdateServiceYuYue;
	private ArrayList<OrdersListBean> lists;
	private String pagesize="20";
	private EditText et_find;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
		AppManager.getAppManager().addActivity(this);
		initData();
		setListener();
	}


	public void setCallback(){
		iUpdateServiceYuYue=new IUpdateServiceYuYue() {


			@Override
			public void setServicYuYueList(ArrayList<OrdersListBean> beans) {
				// TODO Auto-generated method stub
				lists.addAll(beans);
				adapter.addList(beans);
				adapter.notifyDataSetChanged();
				page++;
			}
		};
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		commonTitleBar.setCenterText(getString(R.string.yuyue_title));
		commonTitleBar.setReturnVisible();
		mPullToRefreshListView
		.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(
					PullToRefreshBase<ListView> refreshView) {
				new GetOrdersListAsyncTask(mAct, SharedPreferencesUtil.getInstance(mAct).getUserId(), page+"", pagesize).execute("");
			}
		});
		//		new GetDataTask(mAct).execute("");
		setCallback();
		listView.setAdapter(adapter);
		String s= SharedPreferencesUtil.getInstance(mAct).getUserId();
		if(!TextUtils.isEmpty(s))
		{
			new GetOrdersListAsyncTask(mAct, SharedPreferencesUtil.getInstance(mAct).getUserId(), page+"", pagesize).execute("");
		}else{
			ToastAlone.showToast(mAct,getString(R.string.order_not_login));
		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//		MyObserver.unregiserOberverBitmap();
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_service_yuyue);
		mAct = this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		mPullToRefreshListView=(PullToRefreshListView) findViewById(R.id.lv_yuyue);
		mPullToRefreshListView.setMode(Mode.PULL_FROM_END);
		listView=mPullToRefreshListView.getRefreshableView();
		lists=new ArrayList<OrdersListBean>();
		adapter=new ServiceYuYueListAdapter(mAct, lists);
		et_find=(EditText)findViewById(R.id.et_find);
	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		et_find.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				String temp=s.toString();
				ArrayList<OrdersListBean> tempLists=new ArrayList<OrdersListBean>();
				if(!TextUtils.isEmpty(temp))
				{
					for(int i=0;i<lists.size();i++)
					{
						//if((serviceCarlists.get(i).distance.contains(temp))||(serviceCarlists.get(i).title.contains(temp))){
						if(AppConstant.URLDecoderString(lists.get(i).parkingname).contains(temp)){
							tempLists.add(lists.get(i));
						}else if(TimeUtils.parseLongToDate(lists.get(i).addtime,"yyyy-MM-dd HH:mm").contains(temp)){
							tempLists.add(lists.get(i));
						}
					}
					adapter.resetData(tempLists);
					adapter.notifyDataSetChanged();
				}else{
					adapter.resetData(lists);
					adapter.notifyDataSetChanged();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

	}

	public interface IUpdateServiceYuYue{
		public void setServicYuYueList(ArrayList<OrdersListBean> lists);
	}
}
